//
//  WPDetailWalletViewController.m
//  ZhongChouPingTai
//
//  Created by WeiPan on 16/4/1.
//  Copyright © 2016年 WPJoe. All rights reserved.
//

#import "WPDetailWalletViewController.h"
#import "WPWalletDetailTableViewCell.h"
#import "WPWalletDetailModel.h"
#import "WPWalletDetailViewModel.h"

static NSString *const kUITableViewCellIdentifier = @"cellIdentifier";

@interface WPDetailWalletViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView * msgTableView;/**< 明细列表 */
@property (nonatomic, strong) NSMutableArray * msgDataSource;/**< 明细数据源 */
@property (nonatomic, strong) UILabel * noMoreLabel;/**< 没有说明 */
@property (nonatomic, assign) NSInteger currentPage;/**< 当前页码 */
@property (nonatomic, assign) NSInteger totalsPages;/**< 总页码 */
- (void)initializeDataSource; /**< 初始化数据源 */
- (void)initializeUserInterface; /**< 初始化用户界面 */
- (void)refreshData;/**< 刷新数据 */


@end

@implementation WPDetailWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeDataSource];
    [self initializeUserInterface];
}
#pragma mark - init
- (void)initializeDataSource {
    self.msgDataSource = [NSMutableArray arrayWithCapacity:0];
    self.currentPage = 1;
    self.totalsPages = 1;
}
- (void)initializeUserInterface {
    [self.view addSubview:self.msgTableView];
    __weak typeof(self)weakself = self;
    _msgTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        [weakself refreshData];
    }];
    _msgTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _currentPage++;
        [weakself refreshData];
    }];
    [_msgTableView.header beginRefreshing];
    
}
#pragma mark - 刷新数据
- (void)refreshData {
    if (_currentPage > _totalsPages) {
        [_msgTableView.footer noticeNoMoreData];
        return;
    }
    __weak typeof(self)weakself = self;
    [WPWalletDetailViewModel getWalletDetailWithPageNumber:_currentPage SuccessBlock:^(NSArray *resultArr, NSInteger totalPages) {
        _totalsPages = totalPages;
        if (_currentPage == 1 && _msgDataSource.count) {
            [_msgDataSource removeAllObjects];
        }
        for (WPWalletDetailModel * model in resultArr) {
            [_msgDataSource addObject:model];
        }
        [_msgTableView reloadData];
        if (_msgDataSource.count) {
            if (weakself.noMoreLabel.superview) {
                [weakself.noMoreLabel removeFromSuperview];
            }
        } else {
            if (!weakself.noMoreLabel.superview) {
                [weakself.view addSubview:weakself.noMoreLabel];
            }
        }
        [weakself endRefresh];
    } andWithFailureBlock:^(NSString *fail) {
        if (_currentPage == 1 && [fail isEqualToString:@"无数据"]) {
            [_msgDataSource removeAllObjects];
            [_msgTableView reloadData];
            if (weakself.noMoreLabel.superview) {
                [weakself.view addSubview:weakself.noMoreLabel];
            }
        }
        [weakself endRefresh];
    }];
}
- (void)endRefresh {
    [_msgTableView.header endRefreshing];
    [_msgTableView.footer endRefreshing];
}
#pragma mark - 协议UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _msgDataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WPWalletDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    if (!cell) {
        cell = [[WPWalletDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kUITableViewCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setModel:_msgDataSource[indexPath.row]];
    return cell;
}
#pragma mark - UITableViewDelegate
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60*DHFlexibleHorizontalMutiplier();
}
#pragma mark - getter
- (UILabel *)noMoreLabel {
    if (!_noMoreLabel) {
        _noMoreLabel = ({
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, 30*DHFlexibleHorizontalMutiplier())];
            lab.center = self.view.center;
            lab.font = [UIFont systemFontOfSize:12*DHFlexibleHorizontalMutiplier()];
            lab.textAlignment = NSTextAlignmentCenter;
            lab.text = @"暂无订单信息";
            lab;
        });
    }
    return _noMoreLabel;
}
- (UITableView *)msgTableView {
    if (!_msgTableView) {
        _msgTableView = ({
            UITableView * tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, navBar_Fheight, SCREEN_SIZE.width, SCREEN_SIZE.height - navBar_Fheight) style:UITableViewStylePlain];
            tableview.dataSource = self;
            tableview.delegate = self;
            tableview.tableFooterView = [UIView new];
            tableview.pagingEnabled = NO;
            tableview.separatorColor = COLOR_RGB(211, 211, 211, 1);
            tableview;
        });
    }
    return _msgTableView;
}
@end
